﻿/*********************************************
*Create By：Paine Zeng
*Create Date: 2023/09/03 8:57:52
*Description:
*Email: tozeng@vip.qq.com
*Blog:  https://blog.csdn.net/zengqh0314
*Gitee: https://gitee.com/painezeng
**********************************************/

using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Collections.Generic;

namespace SolidWorks.Base
{
    /// <summary>
    /// 特征树相关操作
    /// </summary>
    public class FeatureTreeHelper
    {
        private int traverseLevel = 0;

        /// <summary>
        /// 读取特征节点树
        /// </summary>
        /// <param name="swModel"></param>
        /// <returns></returns>
        public List<FeaNode> GetFeaNodes(ModelDoc2 swModel)
        {
            List<FeaNode> feaNodes = new List<FeaNode>();

            var featureMgr = swModel.FeatureManager;

            var rootNode = featureMgr.GetFeatureTreeRootItem2((int)swFeatMgrPane_e.swFeatMgrPaneBottom);

            var rootFeaNode = new FeaNode
            {
                IsRoot = true,
                Name = rootNode.Text,
                Expanded = rootNode.Expanded,
                TreeControlItem = rootNode
            };

            feaNodes.Add(rootFeaNode);

            Traverse_node(rootNode, rootFeaNode);

            return feaNodes;
        }

        /// <summary>
        /// 返回根节点树
        /// </summary>
        /// <param name="swModel"></param>
        /// <returns></returns>
        public FeaNode GetRootFeaNode(ModelDoc2 swModel)
        {
            var featureMgr = swModel.FeatureManager;

            var rootNode = featureMgr.GetFeatureTreeRootItem2((int)swFeatMgrPane_e.swFeatMgrPaneBottom);

            var rootFeaNode = new FeaNode
            {
                IsRoot = true,
                Name = rootNode.Text,
                Expanded = rootNode.Expanded,
                TreeControlItem = rootNode,
                FileExt =System.IO.Path.GetExtension(swModel.GetPathName()) 

            };

            Traverse_node(rootNode, rootFeaNode);

            return rootFeaNode;
        }

        /// <summary>
        /// 遍历节点
        /// </summary>
        /// <param name="node"></param>
        /// <param name="FeaNode"></param>
        private void Traverse_node(TreeControlItem node, FeaNode FeaNode)
        {
            string restOfString = "";
            //string indent = "";
            string supprString = "";
            string visString = "";

            var displayNodeInfo = false;
            var nodeObjectType = node.ObjectType;
            var nodeObject = node.Object;

            switch (nodeObjectType)
            {
                case (int)swTreeControlItemType_e.swFeatureManagerItem_Feature:
                    FeaNode.IsFeature=true;
                    displayNodeInfo = true;
                    if ((nodeObject != null))
                    {
                        var featureNode = (Feature)nodeObject;

                        FeaNode.FeatureType = featureNode.GetTypeName();

                        FeaNode.SelectString = featureNode.GetNameForSelection(out string s);

                        restOfString = "[FEATURE: " + featureNode.Name + "]";
                    }
                    else
                    {
                        restOfString = "[FEATURE: object Null?!]";
                    }

                    break;

                case (int)swTreeControlItemType_e.swFeatureManagerItem_Component:

                    displayNodeInfo = true;
                    FeaNode.IsComp = true;
                    if ((nodeObject != null))
                    {
                        var componentNode = (Component2)nodeObject;
                        var compName = componentNode.Name2;

                        FeaNode.SelectString = componentNode.GetSelectByIDString();

                        var filepath = componentNode.GetPathName();
                        if (!string.IsNullOrWhiteSpace(filepath) )
                        {
                            FeaNode.FileExt =System.IO.Path.GetExtension(filepath);
                        }
                       
                        if ((string.IsNullOrEmpty(compName)))
                        {
                            compName = "???";
                        }

                        var suppr = componentNode.GetSuppression();

                        switch ((suppr))
                        {
                            case (int)swComponentSuppressionState_e.swComponentFullyResolved:
                                supprString = "Resolved";

                                break;

                            case (int)swComponentSuppressionState_e.swComponentLightweight:
                                supprString = "Lightweight";

                                break;

                            case (int)swComponentSuppressionState_e.swComponentSuppressed:
                                supprString = "Suppressed";

                                break;
                        }

                        var vis = componentNode.Visible;

                        switch ((vis))
                        {
                            case (int)swComponentVisibilityState_e.swComponentHidden:
                                visString = "Hidden";

                                break;

                            case (int)swComponentVisibilityState_e.swComponentVisible:
                                visString = "Visible";

                                break;
                        }

                        var notfloating = componentNode.IsFixed();

                        //if (notfloating == false)
                        //{
                        //}
                        //else
                        //{
                        //}

                        var componentDoc = componentNode.GetModelDoc2();

                        var docString = "";
                        if (componentDoc == null)
                        {
                            docString = "NotLoaded";
                        }
                        else
                        {
                            docString = "Loaded";
                        }

                        var refConfigName = componentNode.ReferencedConfiguration;

                        if ((string.IsNullOrEmpty(refConfigName)))
                        {
                            refConfigName = "???";
                        }

                        restOfString = "[COMPONENT: " + compName + " " + docString + " " + supprString + " " + visString + " " + refConfigName + "]";
                    }
                    else
                    {
                        restOfString = "[COMPONENT: object Null?!]";
                    }

                    break;

                default:

                    displayNodeInfo = true;

                    if ((nodeObject != null))
                    {
                        restOfString = "[object type not handled]";
                    }
                    else
                    {
                        restOfString = "[object Null?!]";
                    }

                    break;
            }

            //for (var i = 1; i <= traverseLevel; i++)
            //{
            //    indent = indent + "  ";
            //}

            //traverseLevel = traverseLevel + 1;

            var childNode = node.GetFirstChild();

            while ((childNode != null))
            {
                FeaNode childFeaNode = new FeaNode
                {
                    Name = childNode.Text,
                    Expanded = childNode.Expanded
                };

                FeaNode.TreeControlItem = childNode;

                FeaNode.SubNodes.Add(childFeaNode);

                Traverse_node(childNode, childFeaNode);

                childNode = childNode.GetNext();
            }

            //traverseLevel = traverseLevel - 1;
        }
    }

    /// <summary>
    /// 特征节点
    /// </summary>
    public class FeaNode
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 是否根节点
        /// </summary>
        public bool IsRoot { get; set; }

        /// <summary>
        /// 文件类型
        /// </summary>
        public string FileExt { get; set; }

        /// <summary>
        /// 是否展开
        /// </summary>
        public bool Expanded { get; set; }

        /// <summary>
        /// 节点对象
        /// </summary>
        public TreeControlItem TreeControlItem { get; set; }

        /// <summary>
        /// 子节点
        /// </summary>
        public List<FeaNode> SubNodes { get; set; } = new List<FeaNode>();
        /// <summary>
        /// 是否是组件
        /// </summary>
        public bool IsComp { get; set; } = false;

        /// <summary>
        /// 是否是特征
        /// </summary>
        public bool IsFeature { get; set; } = false;

        /// <summary>
        /// 特征类型
        /// </summary>
        public string FeatureType { get; set; } = "";

        /// <summary>
        /// 选择字符串
        /// </summary>
        public string SelectString { get; set; } = "";





    }
}